//Accepted
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=100&category=12&page=show_problem&problem=1008
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
#include <list>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
bool e[10][10][10][10], ok;
int t, i, k, sum, dir[8][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{-1,0,0,0},{0,-1,0,0},{0,0,-1,0},{0,0,0,-1}};
typedef struct pppp{
	int a, b, c, d, sum;
} p;
p x, y, z;

int mod(int x){
	return x<0?x+10:x%10;
}
int main (){
	//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);
	scanf("%d", &t);
	
	while(t--){
		memset(e,0,sizeof(e));
		
		scanf("%d%d%d%d", &x.a, &x.b, &x.c, &x.d);
		scanf("%d%d%d%d", &y.a, &y.b, &y.c, &y.d);
		scanf("%d", &k);
		for(i=0;i<k;++i){
			scanf("%d%d%d%d", &z.a, &z.b, &z.c, &z.d);
			e[z.a][z.b][z.c][z.d]=1;
		}
		queue<p> q;
		x.sum=0;
		e[x.a][x.b][x.c][x.d]=1;
		q.push(x);
		ok=0;
		while(!q.empty()){
			z=q.front();q.pop();
			if(z.a==y.a&&z.b==y.b&&z.c==y.c&&z.d==y.d){
				ok=1;
				break;
			}
			for(i=0;i<8;++i){
				x.a=mod(z.a+dir[i][0]);
				x.b=mod(z.b+dir[i][1]);
				x.c=mod(z.c+dir[i][2]);
				x.d=mod(z.d+dir[i][3]);
				x.sum=z.sum+1;
				if(!e[x.a][x.b][x.c][x.d]){
					e[x.a][x.b][x.c][x.d]=1;
					q.push(x);
				}
			}
		}
		if(ok)printf("%d\n", z.sum);
		else printf("-1\n");
	}
	return 0;
}
